<template>
  <div v-show="this.$store.state.isShowAll" class="newUser">
    <el-form>
      <!-- <el-form-item label="新建新用户"></el-form-item> -->
      <h2>新建用户</h2>
      <el-form-item label="用户名">
        <el-input v-model="userName" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="passWord" placeholder="" show-password></el-input>
      </el-form-item>
      <el-form-item label="权限">

        <el-select v-model="authority" placeholder="请选择">
          <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          >
          </el-option>
        </el-select>

      </el-form-item>
      <el-form-item label="添加头像">
        <el-image
            :src="userIcon"
            fit="contain"
            style="width: 100px; height: 100px">
        </el-image>

        <input id="file"
               accept="image/*"
               style="display: none"
               type="file"
               @change="getPicture($event)"
        />


      </el-form-item>
      <el-form-item class="buttonBox">
        <el-button type="primary" @click="callFile">选择用户头像</el-button>
        <el-button type="primary" @click="addUser">添加</el-button>
        <el-button type="warning"
                   @click="()=>{this.passWord = '';this.userName = ''; this.authority = '',this.userName=''}">取消
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<style scoped>
.newUser {
  display: flex;
  justify-content: center;
  align-items: center;
  height: calc(100vh - 3.75rem);


}

.el-form {
  width: 21.875rem;

}

.el-select {
  display: flex;

}

element.style {
  margin-bottom: 0 !important;
}

.buttonBox {
  display: flex;
  justify-content: center;
}

</style>

<script>
export default {
  name: 'NewUser',

  data() {
    return {
      options: [
        {
          label: '管理员',
          value: 'admin'
        },
        {
          label: '注册用户',
          value: 'singedUser'
        }],
      authority: '',
      userName: '',
      passWord: '',
      userIcon: ''
    }
  },
  methods: {
    addUser() {
      let postData = {
        token: this.$store.state.userToken,
        newUser: {
          UserName: this.userName,
          PassWord: this.passWord,
          Authority: this.authority,
          UserIcon: this.userIcon
        }
      }

      let url = 'http://shilezhi.club:8082/Api/User/AddUser'

      this.axios.defaults.withCredentials = true;
      this.axios.defaults.headers.post["Content-Type"] =
          "application/json;charset=utf-8";
      this.axios.post(url, JSON.stringify(postData)).then(
          response => {
            if (response.data.code === 'ok') {
              this.$message.success('添加成功')
            } else {
              this.$message.error('添加失败' + response.info)
            }
          },
          error => {
            this.$message.error(error)
          }
      )
    },
    getPicture(e) {
      //预览图片
      let src = window.URL.createObjectURL(e.target.files[0]);
      // this.uploadImg.push(src);
      //将图片文件转化成base64格式图片
      var reader = new FileReader();
      reader.onload = (e) => {
        //e.target.result  就是从本地读取的图片的base64格式,将它上传给服务器即可
        //使用axios的post方法上传即可
        console.log(e)
        this.userIcon = e.target.result;

      }
      reader.readAsDataURL(e.target.files[0])
    },
    callFile() {
      //点击添加图片按钮，触发type:"file"的input标签
      let fileDom = document.querySelector("#file")
      fileDom.click()
    }
  }

}

</script>